package com.lmaos.basetools.base.prob;

import java.util.Collection;
import java.util.Random;

import com.lmaos.basetools.base.SNumberUtils;

public class SProbUtils {
	public static SProb extractOne(Collection<SProb> probs){
		return extract(probs, 1)[0];
	}
	/** 通过指定概率返回指定数量的结果
	 * 
	 * @param probs
	 * @return
	 */
	public static SProb[] extract(Collection<SProb> probs,int size){
		adjustment(probs);
		SProb many[] = new SProb[size];
		int total = countTotal(probs);
		if(total == 0){
			return many;
		}
		for (int i = 0; i < many.length; i++) {
			int index = new Random().nextInt(total);
			for (SProb sProb : probs) {
				if(sProb.getMin()<=index&&sProb.getMax()>=index){
					many[i] = sProb;
				}
			}
		}
		return many;
	}
	public static SProb extractOne(SProb[] probs){
		return extract(probs, 1)[0];
	}
	/** 通过指定概率返回指定数量的结果
	 * 
	 * @param probs
	 * @return
	 */
	public static SProb[] extract(SProb[] probs,int size){
		adjustment(probs);
		SProb many[] = new SProb[size];
		int total = countTotal(probs);
		if(total == 0){
			return many;
		}
		for (int i = 0; i < many.length; i++) {
			int index = new Random().nextInt(total);
			for (SProb sProb : probs) {
				if(sProb.getMin()<=index&&sProb.getMax()>=index){
					many[i] = sProb;
				}
			}
		}
		return many;
	}
	
	public static void adjustment(Collection<SProb> probs){
		int index = 0;
		for (SProb prob : probs) {
			if(prob.getProb()>0){
				prob.setMin(index);
				prob.setMax(index+prob.getProb()-1);
				index = index+prob.getProb();
			}
		}
	}
	public static void adjustment(SProb[] probs){
		int index = 0;
		for (SProb prob : probs) {
			prob.setMin(index);
			prob.setMax(index+prob.getProb()-1);
			index = index+prob.getProb();
		}
	}
	public static SProb[] toArrays(Collection<SProb> probs){
		int index = 0;
		SProb[] sProbs = new SProb[probs.size()];
		for (SProb sProb : probs) {
			sProbs[index++] = sProb;
		}
		return sProbs;
	}
	public static int countTotal(Collection<SProb> probs){
		int total = 0;
		for (SProb sProb : probs) {
			total+=sProb.getProb();
		}
		return total;
	}
	public static int countTotal(SProb[] probs){
		int total = 0;
		for (SProb sProb : probs) {
			total+=sProb.getProb();
		}
		return total;
	}


	public static int toProb(String text){
		text = text.replace(".", "");
		return SNumberUtils.toInt(text);
	}
	public static void main(String[] args) {
//		System.out.println(toProb("0.10"));
		int qq = 0;
		int nn = 0;
		int total = 1000000;
//		for (int i = 0; i < total; i++) {
//			String value = extractOne(SProb.createSProbs(new SProb(2500, "nn"),new SProb(7500, "qq"))).toString();
//			if(value.equals("qq")){
//				qq++;
//			}else{
//				nn++;
//			}
//		}
		SProb[] probs = extract(SProb.createSProbs(new SProb("10", "nn"),new SProb("90", "qq")),total);
		for (SProb prob : probs) {
			String value = String.valueOf(prob);
			if(value.equals("qq")){
				qq++;
			}else if(value.equals("nn")){
				nn++;
			}
		}
		
		System.out.println("qq:"+qq+"/"+total);
		System.out.println("nn:"+nn+"/"+total);
		
	}
}
